//################################
// 版权所有：鸽子游戏俱乐部
// 系统名称：鸽子游戏俱乐部-移动版
// 文件名称：index.js
// 创建时间：2016年12月04日
// 创建人员：Panda
// 功能说明：redux logger将错误的信息记入本地存储
//################################

'use strict';

import LocalStorage from '../../utils/storageUtil';
import UUID from 'react-native-uuid';

const pandaLogger = store => next => action => {
    if (endsWithFailed(action.type)) {
        let errorLog = Object.assign({}, action, {
            time: getNowFormatDate(),
            id: UUID.v1(),
        });
        console.log('panda dispatching', errorLog);
        LocalStorage.saveErrorLog(errorLog);
    }
    let result = next(action);
    console.log('panda next state', store.getState());
    return result;
};

function endsWithFailed(str){ 
    var reg1 = new RegExp("FALIED$");
    var reg2 = new RegExp("FAILED$");
    return (reg1.test(str) || reg2.test(str)); 
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}


export default pandaLogger;